package com.lmn.jpa;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface PersonRepository extends CrudRepository<PersonPO, Long> {

    @Query(name = "queryByName", nativeQuery = true, value = "select * from person where name =:name")
    List<PersonPO> queryByName(@Param("name") String name);

    @Transactional
    @Modifying
    @Query(value = "update person set name = ?1 where id = ?2", nativeQuery = true)
    int updateName(String name, Long id);

    List<PersonPO> findPersonPOByNameLike(String name);

    Page<PersonPO> findAll(Specification<PersonPO> name, Pageable pageable);

    @Query(value = "SELECT new com.lmn.jpa.PersonInfo(p, pw) FROM person p left join person_work pw on p.id = pw.id")
    List<PersonInfo> findPersonInfo();

}
